using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using NTTHAO.Common.BL;

public partial class admin_webparts_Language_LanguageList : System.Web.UI.UserControl
{
    public int _PageNo = 1;
    public int _PageCount = 1;
    public int _PageSize = Constant.NUM_ROW_PER_PAGE;
    public long _TotalItem = 0;
    public int _SortType = 0; /* =0: no sort, =1: increment, =2: decrement */
    public string _SortValue = "";
    public int[] _PageSizeArray = { 10, 20, 50, 100, 200 };

    public DataTable _PageValues = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Admin_Config_PageSize"] != null)
        {
            _PageSize = (int)Session["Admin_Config_PageSize"];
        }

        if (Request.QueryString["pagesize"] != null)
        {
            try
            {
                _PageSize = Int32.Parse(Request.QueryString["pagesize"]);
                for (int i = 0; i < _PageSizeArray.Length; i++)
                {
                    if (_PageSize == _PageSizeArray[i])
                    {
                        break;
                    }
                    else if (_PageSize < _PageSizeArray[i])
                    {
                        _PageSize = _PageSizeArray[i];
                        break;
                    }
                    else if (i >= _PageSizeArray.Length - 1)
                    {
                        _PageSize = _PageSizeArray[i];
                        break;
                    }
                }

                Session.Remove("Admin_Config_PageSize");
                Session.Add("Admin_Config_PageSize", _PageSize);
            }
            catch (Exception)
            {
            }
        }

        if (Request.QueryString["pageno"] != null)
        {
            try
            {
                _PageNo = Int32.Parse(Request.QueryString["pageno"]);
            }
            catch (Exception)
            {
                _PageNo = 1;
            }
        }

        //SortValue
        if (Session["Admin_Config_SortValue"] != null)
        {
            _SortValue = (string)Session["Admin_Config_SortValue"];
        }
        else
        {
            _SortValue = LanguageText.NAME__COLUMN_NAME;
        }
        string SortColumn = Request.QueryString["sort"];
        if (SortColumn != null &&
                (SortColumn.ToUpper().CompareTo(LanguageText.NAME__COLUMN_NAME.ToUpper()) == 0
                    || SortColumn.ToUpper().CompareTo(LanguageText.TEXT__COLUMN_NAME.ToUpper()) == 0
                    || SortColumn.ToUpper().CompareTo(LanguageText.TYPE__COLUMN_NAME.ToUpper()) == 0))
        {
            _SortValue = SortColumn;
            Session.Remove("Admin_Config_SortValue");
            Session.Add("Admin_Config_SortValue", _SortValue);
        }
        // End SortValue

        // SortType
        if (Session["Admin_Config_SortType"] != null)
        {
            _SortType = (int)Session["Admin_Config_SortType"];
        }
        else
        {
            _SortType = 1;
        }
        if (Request.QueryString["sorttype"] != null)
        {
            try
            {
                _SortType = Int32.Parse(Request.QueryString["sorttype"]);
                if (_SortType != 1 && _SortType != 2)
                {
                    _SortType = 1;
                }
                Session.Remove("Admin_Config_SortType");
                Session.Add("Admin_Config_SortType", _SortType);
            }
            catch (Exception)
            {
                _SortType = 1;
            }
        }
        // End SortType

        if (!IsPostBack)
        {
            BindData();
        }
    }

    public void BindData()
    {
        LanguageText languageText = new LanguageText();
        DataSet ds = languageText.GetEditableList(LanguageUtility.GetLanguageID(Request, Session), _SortValue, _SortType, _PageNo, _PageSize);
        _PageValues = ds != null ? ds.Tables[0] : null;
        _TotalItem = languageText.EditableRecordCount(LanguageUtility.GetLanguageID(Request, Session));
        this._PageCount = (int)(this._TotalItem / this._PageSize + (this._TotalItem % this._PageSize != 0 ? 1 : 0));
    }
}
